package com.wdb.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wdb.exception.BadRequestException;
import com.wdb.utils.enums.DataScopeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;

import java.util.List;

/**
 * @author: wendaobai@qq.com
 * @date: 2024/1/18 0018 14:54
 */
@Slf4j
public class SecurityUtils {
  /**
   * 获取当前登录用户信息
   *
   * @return 获取当前登录用户信息
   */
  public static UserDetails getCurrentUser() {
    UserDetailsService userDetailsService = SpringContextHolder.getBean(UserDetailsService.class);
    return userDetailsService.loadUserByUsername(getCurrentUsername());
  }

  /**
   * 获取系统用户ID
   *
   * @return 系统用户ID
   */
  public static Long getCurrentUserId() {
    UserDetails currentUser = getCurrentUser();

    // 将 Java 对象转换为 JSONObject 对象
    JSONObject jsonObject = (JSONObject) JSON.toJSON(currentUser);

    return jsonObject.getJSONObject("user").getLong("id");
  }

  /**
   * 获取系统用户名称
   *
   * @return 获取系统用户名称
   */
  public static String getCurrentUsername() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) {
      throw new BadRequestException(HttpStatus.UNAUTHORIZED, "当前登录状态过期");
    }
    if (authentication.getPrincipal() instanceof UserDetails) {

      UserDetails userDetails = (UserDetails) authentication.getPrincipal();
      return userDetails.getUsername();
    }
    throw new BadRequestException(HttpStatus.UNAUTHORIZED, "找不到当前登录的信息");
  }
  /**
   * 获取数据权限级别
   *
   * @return 级别
   */
  public static String getDataScopeType() {
    List<Long> dataScopes = getCurrentUserDataScope();
    if (dataScopes.size() != 0) {
      return "";
    }
    return DataScopeEnum.ALL.getValue();
  }

  private static List<Long> getCurrentUserDataScope() {
    UserDetails userDetails = getCurrentUser();
    JSONObject jsonObject = (JSONObject) JSON.toJSON(userDetails);
    log.info("userDetails:{}", jsonObject);
    JSONArray dataScopes = jsonObject.getJSONArray("dataScopes");
    List<Long> list = JSON.parseArray(dataScopes.toJSONString(), long.class);
    return list;
  }
}
